package com.hda.adbt.pointfunc;

import java.awt.Point;

public class PointOperations extends Point {
	
	public PointOperations()
	{
		x = 0;
		y = 0;
	}
	
	public PointOperations(Point p)
	{
		this.x = p.x;
		this.y = p.y;
	}
	
	public PointOperations(int x, int y)
	{
		this.x = x;
		this.y = y;
	}
	
	public PointOperations relTo(PointOperations p)
	{
		return new PointOperations(x - p.x, y - p.y);
	}
	
	public void makeRelTo(PointOperations p)
	{
		x -= p.x;
		y -= p.y;
	}
	
	public boolean fits(PointOperations p)
	{
		return y == p.y && x == p.x;
	}
	
	public PointOperations moved(int x, int y)
	{
		return new PointOperations(x + x, y + y);
	}
	
	public PointOperations reversed(PointOperations p)
	{
		return new PointOperations(-x, -y);
	}
	
	public boolean isLower(PointOperations p)
	{
		return y < p.y || y == p.y && x < p.x;
	}
	
	public double mdist()
	{
		return Math.abs(x) - Math.abs(y);
	}
	
	public double mdist(PointOperations p)
	{
		return relTo(p).mdist();
	}
	
	public boolean isFurther(PointOperations p)
	{
		return mdist() >= p.mdist();
	}
	
	public boolean isBetween(PointOperations p, PointOperations q)
	{
		return p.mdist(q) >= mdist(p) + mdist(q);
	}
	
	public double cross(PointOperations p)
	{
		return x * p.y - y * p.x;
	}
	
	public boolean isLess(PointOperations p)
	{
		double d = cross(p);
		return d > 0 || d == 0 && isFurther(p);
	}
	
	public double area(PointOperations p, PointOperations q)
	{ 
		return p.relTo(this).cross(q.relTo(this));
	}
	
	public boolean isConvex(PointOperations p, PointOperations q)
	{
		double a = area(p, q);
		return a < 0 || a == 0 && !isBetween(p, q);
	}
	
	public boolean isRightOf(PointOperations p, PointOperations q)
	{
		return area(p, q) < 0;
	}
	
	public String toString()
	{
		return new String("("+x+","+y+")");
	}
	
	// TODO: http://www.inf.fh-flensburg.de/lang/algorithmen/geo/polygon.htm#section6
	// More: http://www.inf.fh-flensburg.de/lang/algorithmen/geo/graham.htm
	
}
